﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MVCCentral.Framework.Logger
{
    public class FileLogger : ILogger
    {

        #region ctors
        public FileLogger()
        {
        }
        #endregion

        #region ILogger Members

        /// <summary>
        /// This class and method are used for both the FileError and FileTrace Log Types.
        /// The args.LogType value is concatenated into the filename to write the type of log file.
        /// </summary>
        /// <param name="args"></param>
        public void Log(LogEventArgs args)
        {
            string _fileName;
            _fileName = AppDomain.CurrentDomain.BaseDirectory + @"\Logs\" + args.LogType + "s-" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".txt";
            string msg = "[" + DateTime.Now.ToString() + "] Source: " + args.Source + " - Message: " + args.Message;
            if (args.LogType == "FileError")
            {
                if (args.Exception != null)
                {
                    msg += Environment.NewLine + "   Message:          " + args.Exception.Message;
                    msg += Environment.NewLine + "   StackTrace:       " + args.Exception.StackTrace;
                    msg += Environment.NewLine + "   Target Site:      " + args.Exception.TargetSite.ToString();
                    msg += Environment.NewLine + "   Exception Source: " + args.Exception.Source;
                    msg += Environment.NewLine;
                }
            }

            if (args.LogType == "FileTrace" || args.Exception == null)
            {
                FileWriter writer = new FileWriter(_fileName);
                writer.Write(msg);
            }

        }

        #endregion
    }
}
